<?php 
class Form
{
	public $Id;
	public $Title;
	public $Method;
	public $Valid;
	public $HumanCheck;
	public $Fields;
	
	public function getId() { return $this->Id;  }
	public function getTitle() { return $this->Title; }
	public function getMethod() { return $this->Method; }
	public function getValid() { return $this->Valid; }
	public function getHumanCheck() { return $this->HumanCheck; }
	public function getFields() { return $this->Fields; }
	
	public function setId($val) { $this->Id=$val;  }
	public function setTitle($val) { $this->Title=$val; }
	public function setMethod($val) { $this->Method=$val; }
	public function setValid($val) { $this->Valid=$val; }
	public function setHumanCheck($val) { $this->HumanCheck=$val; }
	public function setFields($val) { $this->Fields=$val; }
	
	public function getFieldsHTML($txt, $format='fieldset')
	{
		$out = '';
		if($this->Fields)
		{
			foreach($this->Fields as $field)
			{ $out .= $field->getHTML($txt, $format); }
		}
		if($this->HumanCheck > 0)
		{
			//$out .= 'Capthca';
		}
		return $out;
	}
	
	public function validate($txt, $com)
	{
		if($this->Fields)
		{
			$this->Valid = true;
			foreach($this->Fields as $field)
			{
				$field->Valid = true;
				$field->setError('');
				
				if((!$field->Value) && ($field->Req) && (!($_REQUEST[$field->Name])))
				{ 
					$field->setError(ucfirst($txt->getWord('missing')));
					$field->Valid = false;
				}
				else
				{
					if($_REQUEST[$field->Name])
					{
						$field->Value = trim($_REQUEST[$field->Name]);
						if(($field->Nospace) && ($com->hasSpace($field->Value)) && ($field->Valid))
						{ 
							$field->setError(ucfirst($txt->getWord('no_space_allowed')));
							$field->Valid = false;
							$com->stripSpace(&$field->Value);
						}
						if(($field->MinLen > 0) && (strlen($field->Value) < $field->MinLen) && ($field->Valid))
						{ 	
							$field->setError(ucfirst($txt->getWord('must_have_min').' '.$field->MinLen.' '.$txt->getWord('chars')));
							$field->Valid = false;
						}
						if(($field->MaxLen > 0) && (strlen($field->Value) > $field->MaxLen) && ($field->Valid))
						{ 
							$field->setError(ucfirst($txt->getWord('can_have_max').' '.$field->MaxLen.' '.$txt->getWord('chars')));
							$field->Valid = false;
							$field->Value = substr($field->Value, 0, $field->MaxLen);
						}
						if(($field->ValidType == 'email') && ($com->isEmail($field->Value) == false) && ($field->Valid))
						{ 
							$field->setError(ucfirst($txt->getWord('invalid').' '.$txt->getWord('e-mail')));
							$field->Valid = false;
						}
						if(($field->ValidType == 'anum') && ($field->Valid) && ($com->stripNonAlphaNum($field->Value) != $field->Value))
						{ 
							$field->setError(ucfirst($txt->getWord('valid_chars_anum')));
							$field->Valid = false;
							$com->stripNonAlphaNum(&$field->Value);
						}
						if(($field->ValidType == 'alpha') && ($field->Valid) && ($com->stripNonAlpha($field->Value) != $field->Value))
						{ 
							$field->setInfo(ucfirst($txt->getWord('valid_chars_alpha')));
							$field->Valid = false;
							$com->stripNonAlpha(&$field->Value);
						}
						if(($field->Compare != '') && ($field->Value != $_REQUEST[$field->Compare]) && ($field->Valid))
						{ 
							$field->setError(ucfirst($txt->getWord('values_do_not_match')));
							$field->Valid = false;
						}
					}
				}
				//echo 'Field: '.$field->Name.' Type: '.$field->Type.'<br>';
				if($field->Valid == false)		
				{ $this->Valid = false; }
			}
		}
		return $this->Valid;
	}
}

class FormField
{
	public $Id;
	public $Name;
	public $Type;
	public $Value;
	public $MinLen;
	public $MaxLen;
	public $Params;
	public $Class;
	public $Label;
	public $Show;
	public $Req;
	public $Nospace;
	public $ValidType;
	public $Compare;
	public $Error;
	public $Info;
	public $Valid;
	public $SrcTable;
	public $SrcLblSelect;
	public $SrcValSelect;
	public $SrcWhere;
	public $SrcValDef;
	public $Options;
	public $Attributes;
	
	public function FormField($name='', $ty='', $lbl='', $show=true, $req=false, $nospace=false, $validt='anum', $mnl=0, $mxl=0, $comp='', $val='', $params=Array()) 
	{ 
		$this->Name = $name;
		$this->Type = $ty;
		$this->Label = $lbl;
		$this->Show = $show;
		$this->Req = $req;
		$this->Nospace = $nospace;
		$this->ValidType = $validt;
		$this->MinLen = $mnl;
		$this->MaxLen = $mxl;
		$this->Compare = $comp;
		if(is_array($params) && (count($params) > 0))
		{ $this->Params = $params; }
		if($val != '')
		{ $this->Value = $val; }
		if($_REQUEST[$this->Name] != '')
		{ $this->Value = trim($_REQUEST[$this->Name]); }
	}
	
	public function getId() { return $this->Id;  }
	public function getName() { return $this->Name; }
	public function getType() { return $this->Type; }
	public function getValue() { return $this->Value; }
	public function getMaxLen() { return $this->MaxLen; }
	public function getMinLen() { return $this->MinLen; }
	public function getParams() { return $this->Params; }
	public function getClass() { return $this->Class; }
	public function getLabel() { return $this->Label; }
	public function getShow() { return $this->Show; }
	public function getReq() { return $this->Req; }
	public function getNospace() { return $this->Nospace; }
	public function getValidType() { return $this->ValidType; }
	public function getCompare() { return $this->Compare; }
	public function getError() { return $this->Error; }
	public function getInfo() { return $this->Info; }
	public function getValid() { return $this->Valid; }
	public function getSrcTable() { return $this->SrcTable; }
	public function getSrcLblSelect() { return $this->SrcLblSelect; }
	public function getSrcValSelect() { return $this->SrcValSelect; }
	public function getSrcWhere() { return $this->SrcWhere; }
	public function getSrcValDef() { return $this->SrcValDef;  }
	public function getOptions() { return $this->Options;  }
	
	public function setId($val) {$this->Id=$val;  }
	public function setName($val) {$this->Name=$val;  }
	public function setType($val) {$this->Type=$val;  }
	public function setValue($val) {$this->Value=$val;  }
	public function setMaxLen($val) {$this->MaxLen=$val;  }
	public function setMinLen($val) {$this->MinLen=$val;  }
	public function setParams($val) {$this->Params=$val;  }
	public function setClass($val) {$this->Class=$val;  }
	public function setLabel($val) {$this->Label=$val;  }
	public function setShow($val) {$this->Show=$val;  }
	public function setReq($val) {$this->Req=$val;  }
	public function setNospace($val) {$this->Nospace=$val;  }
	public function setValidType($val) {$this->ValidType=$val;  }
	public function setCompare($val) {$this->Compare=$val;  }
	public function setError($val) { $this->Error=$val;  }
	public function setInfo($val) { $this->Info=$val;  }
	public function setValid($val) { $this->Valid=$val;  }
	public function setSrcTable($val) { $this->SrcTable=$val;  }
	public function setSrcLblSelect($val) { $this->SrcLblSelect=$val;  }
	public function setSrcValSelect($val) { $this->SrcValSelect=$val;  }
	public function setSrcWhere($val) { $this->SrcWhere=$val;  }
	public function setSrcValDef($val) { $this->SrcValDef=$val;  }
	public function setOptions($val) { $this->Options=$val;  }
	
	public function addOption($value, $text, $txt)
	{
		$this->Options[$value]['value'] = $value;
		$this->Options[$value]['text'] = $text;
		$this->Options[$value]['txt'] = $txt;
	}
	
	public function getHTML($txt, $format='fieldset')
	{	
		if($this->Show)
		{
			if($this->Class == '')
			{ $this->Class = $this->Type; }
			if($this->Type == 'password')
			{ $this->Class .= ' text'; }
			if($this->ValidType == 'num')
			{ $this->Class .=' numeric'; }
			
			$out = '';
			
			if($this->Label)
			{ 
				if($format == 'fieldset')
				{ $out .= '<label for="'.$this->Name.'">'; }
				else
				{ $out .= '<td class="label">'; }
				if(is_array($this->Label))
				{
					$i=0;
					foreach($this->Label as $lbl)
					{
						if($i == 0)
						{ $out .= ucfirst($txt->getWord($lbl)); }
						else
						{ $out .= ' '.$txt->getWord($lbl); }
						$i++;
					}
				}
				else
				{ $out .= ucfirst($txt->getWord($this->Label)); }
				
				if($this->Req)
				{ $out .= '&nbsp;<span class="req">*</span>'; }
				
				if($format == 'fieldset')
				{ $out .= '</label>'; }
				else
				{ $out .= '</td>'; }
			}
		
			if($format == 'table_lines')
			{ 
				 
				if(($this->Error) || ($this->Info))
				{
					$out .= '<tr><td class="validation" colspan="2">';
					if($this->Error)
					{ $out .= '<span class="error">'.$this->Error.'</span>'; }
					if($this->Info)
					{ $out .= '<span class="info">'.$this->Info.'</span>'; }
					$out .= '</td></tr>'; 
				}
				$out .= '<tr>'; 
			}
			else
			{
				if($this->Error)
				{ $out .= '<span class="error">'.$this->Error.'</span><br />'; }
				if($this->Info)
				{ $out .= '<span class="info">'.$this->Info.'</span><br />'; }
			}
			
			if($format == 'table_lines')
			{ $out .= '<td class="input">'; }
			
			if($this->Type == 'select')
			{
				$out .= '<select name="'.$this->Name.'" ';
				if($this->Id != '')
				{ $out .=  ' id="'.$this->Id.'" '; }
				if($this->Class != '')
				{ $out .=  ' class="'.$this->Class.'" '; }
				if($this->Params)
				{
					$keys = array_keys($this->Params);
					foreach($keys as $key)
					{ $out .= $key.'="'.$this->Params[$key].'" '; }
				}	
				$out .= '>';
				
				if(($this->SrcTable != '') && ($this->SrcValSelect != ''))
				{
					$select = $this->SrcValSelect;
					if($this->SrcLblSelect != '')
					{ $select .= ', '.$this->SrcLblSelect; }
					$sql = 'SELECT '.$select.' FROM '.DB_PERFIX.$this->SrcTable.' '.$this->SrcWhere.';';
					$result = mysql_query($sql);
					if($result)
					{
						while($row = mysql_fetch_array($result))
						{
							$out .= '<option value="'.$row[$this->SrcValSelect].'" ';
							if(($this->Value == $row[$this->SrcValSelect]) || (($this->SrcValDef != '') && ($this->SrcValDef == $row[$this->SrcValSelect]) && ($this->Value == '')))
							{ $out .= ' selected="selected" '; } 
							$out .= '>';
							if($this->SrcLblSelect != '')
							{ $out .= $row[$this->SrcLblSelect]; }
							else
							{ $out .= $row[$this->SrcValSelect]; }
							$out .= '</option>';
						}
					}
				}
				else
				{
					if(is_array($this->Options))
					{
						foreach($this->Options as $option)
						{
							$out .= '<option value="'.$option['value'].'" ';
							if(($this->Value == $option['value']) || (($this->SrcValDef != '') && ($this->SrcValDef == $option['value']) && ($this->Value == '')))
							{ $out .= ' selected="selected" '; } 
							
							$out .= '>';
							
							if($option['txt'])
							{ $out .= ucfirst($txt->getWord($option['txt']));; }
							else
							{ $out .= $option['text']; }
							$out .= '</option>';
						}
					}
				}
				
				$out .= '</select>';
			}
			else
			{
				if(($this->Type == 'radio') || ($this->Type == 'checkbox'))
				{
					if(is_array($this->Options))
					{
						$out .= '<table cellpadding="0" border="0" cellspacing="0">';
						foreach($this->Options as $option)
						{
							$out .= '<tr>';
							$out .= 	'<td valign="bottom" align="right">';
							$out .= 		'<input type="'.$this->Type.'" value="'.$option['value'].'" name="'.$this->Name.'"';
											if(($this->Value == $option['value']) || (($this->SrcValDef != '') && ($this->SrcValDef == $option['value']) && ($this->Value == '')))
											{ $out .= ' checked="checked" '; } 
							$out .=  		' />';
							$out .= 	'</td>';
							$out .= 	'<td valign="bottom" align="left">';
							if($option['txt'])
							{ $out .= ucfirst($txt->getWord($option['txt']));; }
							else
							{ $out .= $option['text']; }
							$out .= 	'</td>';
							$out .= '</tr>';
						}
						
						$out .= '</table>';
					}
				}
				else
				{
					$out .= '<input name="'.$this->Name.'" type="'.$this->Type.'" value="'.$this->Value.'" ';
					if($this->Id != '')
					{ $out .=  ' id="'.$this->Id.'" '; }
					if($this->MaxLen > 0)
					{ $out .=  ' maxlength="'.$this->MaxLen.'" '; }
					if($this->Class != '')
					{ $out .=  ' class="'.$this->Class.'" '; }
					if($this->Params)
					{
						$keys = array_keys($this->Params);
						foreach($keys as $key)
						{ $out .= $key.'="'.$this->Params[$key].'" '; }
					}	
					$out .=  ' />';
				}
			}
			if($format == 'table_lines')
			{ $out .= '</td>'; }
			
			if($format == 'table_lines')
			{ $out .= '</tr>'; }
			else
			{ $out .= '<div class="form-field-v-space">&nbsp;</div>'; }
			return $out;
		}
	}
}
?>